package com.spc.car.data.service.project.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spc.car.common.exception.CustomException;
import com.spc.car.data.domain.project.po.GetProjectTenderingInfoPO;
import com.spc.car.data.domain.project.vo.ProjectTenderingTimeVO;
import com.spc.car.data.entity.project.ProjectTendering;
import com.spc.car.data.mapper.project.ProjectTenderingMapper;
import com.spc.car.data.service.project.ProjectTenderingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 招标项目表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-03-31
 */
@Service
public class ProjectTenderingServiceImpl extends ServiceImpl<ProjectTenderingMapper, ProjectTendering> implements ProjectTenderingService {

    @Override
    public String generateBiddingProjectCode() {
        StringBuffer biddingProjectCode = new StringBuffer();
        //设置日期格式
        SimpleDateFormat dateStyle = new SimpleDateFormat("yyyyMMdd");
        String date = dateStyle.format(new Date(System.currentTimeMillis()));
        int numberOfCode = count(new QueryWrapper<ProjectTendering>()
            .like("code","PT"+date)
        );
        if (numberOfCode > 0){
            numberOfCode++;
            String endNumber = String.format("%03d", numberOfCode);
            biddingProjectCode = new StringBuffer("PT").append(date).append(endNumber);
        }else {
            biddingProjectCode = new StringBuffer("PT").append(date).append("001");
        }
        return String.valueOf(biddingProjectCode);
    }

    @Override
    public ProjectTendering getProjectTenderingInfo(GetProjectTenderingInfoPO getProjectTenderingInfoPO) {
        return getOne(new LambdaQueryWrapper<ProjectTendering>()
                .eq(ObjectUtils.isNotEmpty(getProjectTenderingInfoPO.getProjectTenderingId()), ProjectTendering::getId, getProjectTenderingInfoPO.getProjectTenderingId())
                .eq(StringUtils.isNotBlank(getProjectTenderingInfoPO.getProjectTenderingCode()), ProjectTendering::getCode, getProjectTenderingInfoPO.getProjectTenderingCode())
        );
    }

    @Override
    public ProjectTendering getByCode(String projectTenderingCode) {
        if (StringUtils.isBlank(projectTenderingCode)) {
            return null;
        }
        return getOne(new LambdaQueryWrapper<ProjectTendering>()
                .eq(ProjectTendering::getCode, projectTenderingCode));
    }

    @Override
    public boolean updateProjectDate(ProjectTenderingTimeVO projectTenderingTimeVO) {
        ProjectTendering projectTendering = getById(projectTenderingTimeVO.getId());
        if (null == projectTendering) {
            throw new CustomException("没有匹配的招标项目");
        }

        BeanUtils.copyProperties(projectTenderingTimeVO, projectTendering);
        return updateById(projectTendering);
    }
}
